<%@ page language="java" contentType="text/html; charset=UTF-8"	 pageEncoding="UTF-8"%>
<%@ include file="../include/laypublic.jsp"%>
<!DOCTYPE html>
<html>
<head>
  <title>节日管理</title>
</head>
<body>
  <div class="layui-fluid">   
    <div class="layui-card">
      <!-- 查询区域  -->
      <div id="query-form" class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-inline">
              <select id="query-form-bisworkdate" name="bisworkdate" >
              	<option value="">请选择-类型</option>
              	<option value="1">工作日</option>
              	<option value="0">休息日</option>
              </select>
            </div>
          </div>
          <div class="layui-inline">
             <label class="layui-form-label">日期</label>
             <div class="layui-input-inline">
               <input type="text" class="layui-input" id="sent-laydate-start" readonly name="startDate" placeholder="开始日期">
             </div>
             <div class="layui-form-mid">
               -
             </div>
             <div class="layui-input-inline">
               <input type="text" class="layui-input" id="sent-laydate-end" readonly name="endDate" placeholder="结束日期">
             </div>
          </div>
          <shiro:hasPermission name="LIST_HOLIDAY">
	          <div class="layui-inline">
	            <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-holiday-search">
	              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
	            </button>
	            <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-reset">
	              <i class="layui-icon layui-icon-fonts-clear  layuiadmin-button-btn"></i>重置
	            </button>
	          </div>
          </shiro:hasPermission>
        </div>
      </div>
  	  <!-- 按钮区域  -->
      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
        	<shiro:hasPermission name="INIT_HOLIDAY">
          		<button class="layui-btn layuiadmin-btn-admin " data-type="add">
          	 		<i class="layui-icon layui-icon-add-1 layuiadmin-button-btn"></i>初始化
          		</button>
          	</shiro:hasPermission>
          	<shiro:hasPermission name="ZKP_HOLIDAY">
				<button class="layui-btn layuiadmin-btn-admin" onclick="refreshCache()">
          	 		<i class="layui-icon layui-icon-refresh-3 layuiadmin-button-btn"></i>刷新缓存
          		</button>
			</shiro:hasPermission>
        </div>
        <!-- 数据表单 -->
        <table id="LAY-holiday-manage" lay-filter="LAY-holiday-manage"></table>
        
        <div class="layui-form" lay-filter="add_from" id="add_from" style="padding: 20px 30px 0 0;display:none">
		    <div class="layui-form-item">
		      <label class="layui-form-label">年份</label>
		      <div class="layui-input-inline">
		        <input type="text" name="year" maxlength="4" lay-verify="required|number" placeholder="请输入年份" autocomplete="off" class="layui-input">
		      </div>
		    </div>
		    <div class="layui-form-item layui-hide">
		      <input type="button" lay-submit lay-filter="add_from_submit" id="add_from_submit" value="确认">
		    </div>
	  	</div>
      </div>
    </div>
  </div>

  <script>
  layui.config({
    base: '${ctx}/resources/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'table', 'laydate'], function(){
    var $ = layui.$
    ,form = layui.form
    ,laydate = layui.laydate
    ,table = layui.table;
    
  	//开始日期
	var insStart = laydate.render({
  		elem: '#sent-laydate-start'
  		,done: function(value, date){
        	//更新结束日期的最小日期
        	insEnd.config.min = lay.extend({}, date, {
          		month: date.month - 1
        	});
       		//自动弹出结束日期的选择器
       		insEnd.config.elem[0].focus();
  		}
	});

	//结束日期
	var insEnd = laydate.render({
  		elem: '#sent-laydate-end'
  		,done: function(value, date){
    		//更新开始日期的最大日期
    		insStart.config.max = lay.extend({}, date, {
      			month: date.month - 1
    		});
  		}
	});
    // 表单
    table.render({
      elem: '#LAY-holiday-manage'
      ,url: basePath + '/holiday/queryDataByCondition' //请求数据接口
      ,cols: [[
        {type: 'checkbox', fixed: 'left'}
        ,{field: 'dcurrentdate', title: '日期', templet: function(d){
        		return layui.util.toDateString(d.dcurrentdate,"yyyy-MM-dd");
        	}
        }
        ,{field: 'bisworkdate',title: '类型', templet: function(d){
        		return d.bisworkdate == 1 ? "工作日":"休息日";
    		}
        }
        ,{title: '操作', width: 150, align:'center', fixed: 'right', templet: function(d){
	      	  	var optStr ="";
	      	  	<shiro:hasPermission name="UPDATE_HOLIDAY">
	      	  		if(d.bisworkdate == 1){//如果是工作日
		      			optStr += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="rest"><i class="layui-icon layui-icon-edit"></i>设置为休息日</a>';
	      	  		}else{
		      			optStr += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="work"><i class="layui-icon layui-icon-edit"></i>设置为工作日</a>';
	      	  		}
	  	  	  	</shiro:hasPermission>
	  		  	return optStr;
        	}
        }
      ]]
      ,page: true
      ,limit: 10
    });
    
    <shiro:hasPermission name="BUSINESSPARA_QUERY">
	    //监听搜索
	    form.on('submit(LAY-holiday-search)', function(data){
	      //执行重载
	      table.reload('LAY-holiday-manage', {page: {curr: 1},
	        where: trimData(data)
	      });
	    });
	  	//重置清空
	    form.on('submit(LAY-user-back-reset)', function(data){
	    	reset_form('query-form');
	    	//额外处理select
			$("#query-form-bisworkdate").val('');
	    });
    </shiro:hasPermission>
    
    // 监听操作列
    table.on('tool(LAY-holiday-manage)', function(obj){
      var data = obj.data;
      if(obj.event === 'rest'){
          layer.confirm('确定设置为休息日？',{icon:3, title :'提示'}, function(index){
          	$.ajax({
  				type : 'post',
  				url : basePath + '/holiday/setRest?id=' + data.id,
  				success:function(result){
  					if(result.success){
  						table.reload('LAY-holiday-manage'); //数据刷新
  						layer.close(index);
  					}else{
  						layer.msg(result.msg,{icon: 7});
  				  	}
  				}
  			});
          });
      } else if(obj.event === 'work'){
          layer.confirm('确定设置为工作日？',{icon:3, title :'提示'}, function(index){
            	$.ajax({
    				type : 'post',
    				url : basePath + '/holiday/setWork?id=' + data.id,
    				success:function(result){
    					if(result.success){
    						table.reload('LAY-holiday-manage'); //数据刷新
    						layer.close(index);
    					}else{
    						layer.msg(result.msg,{icon: 7});
    				  	}
    				}
    			});
            });
        }
    });
    
    // 按钮事件
    var active = {
      add: function(){
          layer.open({
        	  type: 1
        	  ,title: '初始化'
        	  ,content: $('#add_from')
        	  ,area: layui.admin.screen() < 2 ? ['80%', '180px'] : ['420px','180px']
        	  ,btn: ['确定', '取消']
        	  ,btnAlign: 'c'
        	  ,yes: function(index, layero){
        		  var submitID = 'add_from_submit'
            		,submit = layero.contents().find('#'+ submitID);
        		  //监听提交
        		  form.on('submit('+ submitID +')', function(data){
        			  var field = data.field; //获取提交的字段
        			  //提交 Ajax 成功后，静态更新表格中的数据
        			  $.ajax({
        				  type : 'post',
        				  url : basePath + '/holiday/init',
        				  data: field,
        				  success:function(result){
        					  if(result.success){
        						  table.reload('LAY-holiday-manage'); //数据刷新
        						  layer.close(index); //关闭弹层
        						  clean_form("add_from");//清空表单
        					  }else{
        						  layer.msg(result.msg,{icon: 7});
        					  }
        				  }
              		 });
            	});  
            submit.trigger('click');
          }
        }); 
      }
    }  
    // data-type事件
    $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
  
	//刷新缓存
	function refreshCache(){
		layui.$.ajax({
		  type : 'post',
		  url : '${ctx}/zkpPath/holiday',
		  success:function(result){
			  if(result.success){
				  layer.alert(result.msg, {icon:1});
			  }else{
				  layer.msg(result.msg, {icon:7});
			  }
		  }
	  });
	}
  </script>
</body>
</html>

